package com.test.utils.text;

import com.test.utils.swing.ComponentUtils;
import com.test.utils.swing.SwingParentFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JSONSwing extends SwingParentFrame {

    private static JSONSwing utils;

    private static JTextPane textPane;

    public JSONSwing(){
        super();
    }

    public JSONSwing(String title, Dimension dimension){
        super(title,dimension);
    }

    public static void init(){
        Container container = utils.getContentPane();
        createPanel(container);
    }

    private static void createPanel(Container container) {
        textPane = ComponentUtils.createTextPane(
                "JSON格式化",
                new Rectangle(10, 10, 920, 400),
                new Color(0, 100, 0),
                container
        );

        ComponentUtils.createScrollPane(
                textPane,
                new Rectangle(2,20,980,440),
                BorderFactory.createTitledBorder("JSON格式化"),
                container
        );

        ComponentUtils.createButton(
                "===格式化===",
                new Rectangle(6, 475, 150, 30),
                container,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        String jsonString = textPane.getText().trim();

                        try {
                            textPane.setText(JSONUtils.jsonFormat(jsonString));
                        } catch (Exception exception) {
                            exception.printStackTrace();
                        }
                    }
                }
        );

        ComponentUtils.createButton(
                "===清除===",
                new Rectangle(180, 475, 150, 30),
                container,
                BorderFactory.createLineBorder(Color.gray),
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        textPane.setText("");
                    }
                }
        );
    }

    public static void main(String[] args) {
        utils = new JSONSwing("JSON FORMAT",new Dimension(990,550));
        init();
        textPane.setText(" ");
        textPane.setText("");
        utils.repaint();

    }
}
